#include "personinforshow.h"

#include <QGridLayout>
#include<QPushButton>
#include<QLineEdit>

PersonInforShow::PersonInforShow(QWidget *parent)
    :QDialog(parent)
{
    layout=new QGridLayout();
    // 优化边距和间距设置
    layout->setContentsMargins(40,20,40,20);  // 调整边距，为验证码行留出空间
    layout->setHorizontalSpacing(5);          // 水平间距
    layout->setVerticalSpacing(8);

    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/Resources/windowIcon.png"));
    this->setLayout(layout);
    this->setFixedSize(450,280);  // 增加高度以容纳验证码行
    //设置关闭自动析构
    this->setAttribute(Qt::WA_DeleteOnClose);
    //移动到鼠标的位置
    this->move(QCursor::pos());
    this->setStyleSheet("QWidget{background-color:rgb(255,255,255);font-size:18px;}"
                        "QPushButton{border:none;}QPushButton:pressed{background-color:rgb(235,235,235)}");

    avator=new QPushButton();
    avator->setFixedSize(75,75);
    avator->setIcon(QIcon(":/Resources/avator1.jpg"));
    avator->setIconSize(QSize(75,75));

    //下面框的大小
    QSize size(200,35);      // 内容区域尺寸
    QSize iconSize(30,30);   // 按钮尺寸
    QSize flagSize(40,35);   // 标签尺寸
    QSize verifyCodeSize(120,35); // 验证码输入框尺寸

    //序号相关控件
    numberFlag=new QLabel();
    numberFlag->setFixedSize(flagSize);
    numberFlag->setStyleSheet("QLabel { font-weight: 800; }");
    numberFlag->setText("序号");
    numberFlag->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    numberLabel=new QLabel();
    numberLabel->setText("1234");
    numberLabel->setFixedSize(size);
    numberLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    //昵称相关控件
    nameFlag=new QLabel();
    nameFlag->setText("昵称");
    nameFlag->setFixedSize(flagSize);
    nameFlag->setStyleSheet("QLabel { font-weight: 800; }");
    nameFlag->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    nameShowLabel=new QLabel();
    nameShowLabel->setFixedSize(size);
    nameShowLabel->setText(this->name);
    nameShowLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    nameLineEdit=new QLineEdit();
    nameLineEdit->setFixedSize(size);
    nameLineEdit->hide();

    namebtn=new QPushButton();
    namebtn->setIcon(QIcon(":/Resources/editbtn2.png"));
    namebtn->setFixedSize(iconSize);
    namebtn->setIconSize(iconSize);

    namesubmit=new QPushButton();
    namesubmit->setIcon(QIcon(":/Resources/submit2.png"));
    namesubmit->setIconSize(iconSize);
    namesubmit->setFixedSize(iconSize);
    namesubmit->hide();

    //个性签名相关控件
    signatureFlag=new QLabel();
    signatureFlag->setText("签名");
    signatureFlag->setStyleSheet("QLabel { font-weight: 800;}");
    signatureFlag->setFixedSize(flagSize);
    signatureFlag->setAlignment(Qt::AlignRight| Qt::AlignVCenter);

    signatureShowLabel=new QLabel();
    signatureShowLabel->setFixedSize(size);
    signatureShowLabel->setText(this->signature);
    signatureShowLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    signatureLineEdit=new QLineEdit();
    signatureLineEdit->setFixedSize(size);
    signatureLineEdit->hide();

    signaturebtn=new QPushButton();
    signaturebtn->setIcon(QIcon(":/Resources/editbtn2.png"));
    signaturebtn->setFixedSize(iconSize);
    signaturebtn->setIconSize(iconSize);

    signaturesubmit=new QPushButton();
    signaturesubmit->setIcon(QIcon(":/Resources/submit2.png"));
    signaturesubmit->setIconSize(iconSize);
    signaturesubmit->setFixedSize(iconSize);
    signaturesubmit->hide();

    //电话相关控件
    telephoneFlag=new QLabel();
    telephoneFlag->setText("电话");
    telephoneFlag->setFixedSize(flagSize);
    telephoneFlag->setStyleSheet("QLabel { font-weight: 800;}");
    telephoneFlag->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    telephoneShowLabel=new  QLabel();
    telephoneShowLabel->setFixedSize(size);
    telephoneShowLabel->setText(this->telephone);
    telephoneShowLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    telephoneLineEdit=new QLineEdit();
    telephoneLineEdit->setFixedSize(size);
    telephoneLineEdit->hide();

    telephonebtn=new QPushButton();
    telephonebtn->setIcon(QIcon(":/Resources/editbtn2.png"));
    telephonebtn->setFixedSize(iconSize);
    telephonebtn->setIconSize(iconSize);

    telephonesubmit=new QPushButton();
    telephonesubmit->setIcon(QIcon(":/Resources/submit2.png"));
    telephonesubmit->setIconSize(iconSize);
    telephonesubmit->setFixedSize(iconSize);
    telephonesubmit->hide();

    //验证码相关控件
    indentifyLabel=new QLabel();
    indentifyLabel->setText("验证码");
    indentifyLabel->setFixedSize(60,35);
    indentifyLabel->setStyleSheet("QLabel { font-weight: 800;}");
    indentifyLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    indentifyLabel->hide();

    indentifyLineEdit=new QLineEdit();
    indentifyLineEdit->setFixedSize(verifyCodeSize); // 使用专门的验证码输入框尺寸
    indentifyLineEdit->hide();

    indentifybtn=new QPushButton();
    indentifybtn->setFixedSize(QSize(80, 30)); // 调整验证码按钮尺寸
    indentifybtn->setStyleSheet("QPushButton {"
                                "background-color: #4CAF50;"
                                "color: white;"
                                "border-radius: 4px;"
                                "font-size: 12px;"
                                "}"
                                "QPushButton:pressed {"
                                "background-color: #45a049;"
                                "}");
    indentifybtn->setText("获取验证码");
    indentifybtn->hide();

    // 布局控件
    layout->addWidget(avator,0,0,2,2);

    // 序号行
    layout->addWidget(numberFlag,0,2,1,1, Qt::AlignLeft);
    layout->addWidget(numberLabel,0,3,1,4, Qt::AlignLeft);

    // 昵称行
    layout->addWidget(nameFlag,1,2,1,1, Qt::AlignLeft);
    layout->addWidget(nameShowLabel,1,3,1,4, Qt::AlignLeft);
    layout->addWidget(namebtn,1,7,1,1, Qt::AlignRight);

    // 签名行
    layout->addWidget(signatureFlag,2,2,1,1, Qt::AlignLeft);
    layout->addWidget(signatureShowLabel,2,3,1,4, Qt::AlignLeft);
    layout->addWidget(signaturebtn,2,7,1,1, Qt::AlignRight);

    // 电话行
    layout->addWidget(telephoneFlag,3,2,1,1, Qt::AlignLeft);
    layout->addWidget(telephoneShowLabel,3,3,1,4, Qt::AlignLeft);
    layout->addWidget(telephonebtn,3,7,1,1, Qt::AlignRight);

    // 验证码行 - 使用新的行号
    layout->addWidget(indentifyLabel,4,2,1,1,Qt::AlignLeft);
    layout->addWidget(indentifyLineEdit,4,3,1,2,Qt::AlignLeft); // 只占2列，为按钮留空间
    layout->addWidget(indentifybtn,4,5,1,3,Qt::AlignLeft); // 按钮占据3列，左对齐

    // 设置列宽比例
    layout->setColumnStretch(2, 0);  // flag列不拉伸
    layout->setColumnStretch(3, 1);  // 内容列拉伸
    layout->setColumnStretch(5, 0);  // 验证码按钮列不拉伸
    layout->setColumnStretch(7, 0);  // 编辑按钮列不拉伸

    // 设置行高比例，为验证码行分配空间
    layout->setRowStretch(4, 1);     // 验证码行可拉伸

    //设置信号与槽函数
    connect(namebtn,&QPushButton::clicked,this,[this](){
        //移除我们的label以及修改按钮
        layout->removeWidget(namebtn);
        layout->removeWidget(nameShowLabel);
        nameShowLabel->hide();
        namebtn->hide();
        //添加我们的编辑框进入
        namesubmit->show();
        nameLineEdit->show();
        nameLineEdit->setText(nameShowLabel->text());
        layout->addWidget(nameLineEdit,1,3,1,4, Qt::AlignLeft);
        layout->addWidget(namesubmit,1,7,1,1, Qt::AlignRight);
    }) ;
    connect(namesubmit,&QPushButton::clicked,this,[this](){
        QString text=nameLineEdit->text();
        this->name=text;
        //移除我们的label以及修改按钮
        layout->removeWidget(namesubmit);
        layout->removeWidget(nameLineEdit);
        nameLineEdit->hide();
        namesubmit->hide();
        //添加我们的label进入
        namebtn->show();
        nameShowLabel->show();
        nameShowLabel->setText(text);
        layout->addWidget(nameShowLabel,1,3,1,4, Qt::AlignLeft);
        layout->addWidget(namebtn,1,7,1,1, Qt::AlignRight);
    });

    // 签名的信号与槽连接
    connect(signaturebtn, &QPushButton::clicked, this, [this](){
        // 移除label和编辑按钮
        layout->removeWidget(signaturebtn);
        layout->removeWidget(signatureShowLabel);
        signatureShowLabel->hide();
        signaturebtn->hide();
        // 添加编辑框和提交按钮
        signaturesubmit->show();
        signatureLineEdit->show();
        signatureLineEdit->setText(signatureShowLabel->text());
        layout->addWidget(signatureLineEdit, 2, 3, 1, 4, Qt::AlignLeft);
        layout->addWidget(signaturesubmit, 2, 7, 1, 1, Qt::AlignRight);
    });

    connect(signaturesubmit, &QPushButton::clicked, this, [this](){
        QString text = signatureLineEdit->text();
        this->signature=text;
        // 移除提交按钮和编辑框
        layout->removeWidget(signaturesubmit);
        layout->removeWidget(signatureLineEdit);
        signatureLineEdit->hide();
        signaturesubmit->hide();
        // 添加label和编辑按钮
        signaturebtn->show();
        signatureShowLabel->show();
        signatureShowLabel->setText(text);
        layout->addWidget(signatureShowLabel, 2, 3, 1, 4, Qt::AlignLeft);
        layout->addWidget(signaturebtn, 2, 7, 1, 1, Qt::AlignRight);
    });

    // 电话的信号与槽连接
    connect(telephonebtn, &QPushButton::clicked, this, [this](){
        // 移除label和编辑按钮
        layout->removeWidget(telephonebtn);
        layout->removeWidget(telephoneShowLabel);
        telephoneShowLabel->hide();
        telephonebtn->hide();
        // 添加编辑框和提交按钮
        telephonesubmit->show();
        telephoneLineEdit->show();
        indentifyLabel->show();
        indentifyLineEdit->show();
        indentifybtn->show();
        telephoneLineEdit->setText(telephoneShowLabel->text());
        layout->addWidget(telephoneLineEdit, 3, 3, 1, 4, Qt::AlignLeft);
        layout->addWidget(telephonesubmit, 3, 7, 1, 1, Qt::AlignRight);
    });

    connect(telephonesubmit, &QPushButton::clicked, this, [this](){
        QString text = telephoneLineEdit->text();
        this->telephone=text;
        // 移除提交按钮和编辑框
        layout->removeWidget(telephonesubmit);
        layout->removeWidget(telephoneLineEdit);
        telephoneLineEdit->hide();
        telephonesubmit->hide();
        // 添加label和编辑按钮
        telephonebtn->show();
        telephoneShowLabel->show();
        indentifyLabel->hide();
        indentifyLineEdit->hide();
        indentifybtn->hide();
        telephoneShowLabel->setText(text);
        layout->addWidget(telephoneShowLabel, 3, 3, 1, 4, Qt::AlignLeft);
        layout->addWidget(telephonebtn, 3, 7, 1, 1, Qt::AlignRight);
    });
}
